package com.gzk.oeno.business.loader;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.gzk.oeno.business.appellation.Appellation;
import com.gzk.oeno.business.appellation.AppellationImportMapper;
import com.gzk.oeno.business.cepage.Cepage;
import com.gzk.oeno.business.cepage.CepageImportMapper;
import com.gzk.oeno.business.critere.AbstractCritere;
import com.gzk.oeno.business.critere.AbstractCritereImportMapper;
import com.gzk.oeno.business.critere.Echelle;
import com.gzk.oeno.business.critere.EchelleImportMapper;
import com.gzk.oeno.business.pays.Pays;
import com.gzk.oeno.business.pays.PaysImportMapper;
import com.gzk.oeno.business.typevin.TypeVin;
import com.gzk.oeno.business.typevin.TypeVinImportMapper;
import com.gzk.oeno.dao.DBSave;
import com.gzk.utils.CustomException;
import com.gzk.utils.parsing.AbstractParserMapper;
import com.gzk.utils.parsing.Parser;
import com.gzk.utils.parsing.strategy.ParsingStrategies;

public class GenericDataLoader {

	private static final Map<Class<?>, AbstractParserMapper<?>> MAPPERS = new HashMap<>();
	static{
		MAPPERS.put(Appellation.class, new AppellationImportMapper());
		MAPPERS.put(Pays.class, new PaysImportMapper());
		MAPPERS.put(TypeVin.class, new TypeVinImportMapper());
		MAPPERS.put(Cepage.class, new CepageImportMapper());
		MAPPERS.put(AbstractCritere.class, new AbstractCritereImportMapper());
		MAPPERS.put(Echelle.class, new EchelleImportMapper());
	}
	
	
	private static final String FOLDER = "data/";
	
	private GenericDataLoader(){}
	
	
	public static <T> Collection<T> getDefaultValues(Class<T> bean) throws CustomException{
		String dataFile = FOLDER + bean.getSimpleName() + ".csv";
		@SuppressWarnings("unchecked")
		AbstractParserMapper<T> mapper = (AbstractParserMapper<T>) MAPPERS.get(bean);
		
		return Parser.parseDocument(dataFile, true, ParsingStrategies.CSV, mapper);
	}
	
	
	public static <T> void loadDefaultValues(Class<T> bean) throws CustomException{
		Collection<T> importedData = getDefaultValues(bean);
		DBSave.persistObjects(importedData);
	}
	
}
